﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using Model;
using DevExpress.XtraEditors;


namespace Thep
{
    public partial class FrmKetQua : Form
    {
        public FrmKetQua()
        {
            InitializeComponent();
        }

        #region Database
        OleDbConnection conn;
        OleDbCommand cmd;

        public void Connect()
        {
            string connectstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Database.a;Persist Security Info=False";
            conn = new OleDbConnection(connectstring);
            cmd = conn.CreateCommand();
        }
        #endregion

        

        #region Methods
        private string ketquaID = "";
        public float Alpha;

        #region Find

        public LoaiThepDam FindLoaiThepDam(int t)
        {
            try
            {
                Connect();
                LoaiThepDam thep = new LoaiThepDam();
                cmd.CommandText = "SELECT * FROM LoaiThepDam WHERE LoaiThepDamID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.LoaiThepDamID = int.Parse(reader["LoaiThepDamID"].ToString());
                    thep.TenLoaiThepDam = reader["TenLoaiThepDam"].ToString();
                    thep.ChieuCaoTietDien = float.Parse(reader["ChieuCaoTietDien"].ToString());
                    thep.ChieuDayBanBung = float.Parse(reader["ChieuDayBanBung"].ToString());
                    thep.BeRongBanCanh = float.Parse(reader["BeRongBanCanh"].ToString());
                    thep.ChieuDayBanCanh = float.Parse(reader["ChieuDayBanCanh"].ToString());
                    thep.ChieuCaoDuongHan = float.Parse(reader["ChieuCaoDuongHan"].ToString());
                    thep.ThepToHop = int.Parse(reader["ThepToHop"].ToString());
                }
                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public LoaiThepCot FindLoaiThepCot(int t)
        {
            try
            {
                Connect();
                LoaiThepCot thep = new LoaiThepCot();
                cmd.CommandText = "SELECT * FROM LoaiThepCot WHERE LoaiThepCotID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.LoaiThepCotID = int.Parse(reader["LoaiThepCotID"].ToString());
                    thep.TenLoaiThepCot = reader["TenLoaiThepCot"].ToString();
                    thep.ChieuCaoTietDien = float.Parse(reader["ChieuCaoTietDien"].ToString());
                    thep.ChieuDayBanBung = float.Parse(reader["ChieuDayBanBung"].ToString());
                    thep.BeRongBanCanh = float.Parse(reader["BeRongBanCanh"].ToString());
                    thep.ChieuDayBanCanh = float.Parse(reader["ChieuDayBanCanh"].ToString());
                    thep.ChieuCaoDuongHan = float.Parse(reader["ChieuCaoDuongHan"].ToString());
                    thep.DienTichTietDienCotThep = float.Parse(reader["DienTichTietDienCotThep"].ToString());
                    thep.ThepToHop = int.Parse(reader["ThepToHop"].ToString());
                }
                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public HeSoAnToan FindHeSo(int id)
        {
            try
            {
                Connect();
                HeSoAnToan heso = new HeSoAnToan();
                cmd.CommandText = "SELECT * FROM HeSoAnToan WHERE HeSoAnToanID=" + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    heso.HeSoAnToanID = int.Parse(reader["HeSoAnToanID"].ToString());
                    heso.TenHeSo = reader["TenHeSo"].ToString();
                    heso.KetCauThep = float.Parse(reader["KetCauThep"].ToString());
                    heso.CotThep = float.Parse(reader["CotThep"].ToString());
                    heso.BeTong = float.Parse(reader["BeTong"].ToString());
                    heso.DuongHan = float.Parse(reader["DuongHan"].ToString());
                    heso.BuLong = float.Parse(reader["BuLong"].ToString());
                }
                return heso;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacThepKetCau FindMacThep(int id)
        {
            try
            {
                Connect();
                MacThepKetCau macthep = new MacThepKetCau();
                cmd.CommandText = "SELECT * FROM MacThepKetCau WHERE MacThepKetCauID=" + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    macthep.MacThepKetCauID = int.Parse(reader["MacThepKetCauID"].ToString());
                    macthep.TenMacThep = reader["TenMacThep"].ToString();
                    macthep.BungDam = float.Parse(reader["BungDam"].ToString());
                    macthep.CanhDam = float.Parse(reader["CanhDam"].ToString());
                    macthep.BungCot = float.Parse(reader["BungCot"].ToString());
                    macthep.CanhCot = float.Parse(reader["CanhCot"].ToString());
                    macthep.SuonGoi = float.Parse(reader["SuonGoi"].ToString());
                    macthep.TamDem = float.Parse(reader["TamDem"].ToString());
                    macthep.ModuleDanHoiThepKetCau = float.Parse(reader["ModuleDanHoiThepKetCau"].ToString());
                }
                return macthep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacBeTong FindMacBeTong(int id)
        {
            try
            {
                Connect();
                MacBeTong macbetong = new MacBeTong();
                cmd.CommandText = "SELECT * FROM MacBeTong WHERE MacBeTongID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    macbetong.MacBeTongID = int.Parse(reader["MacBeTongID"].ToString());
                    macbetong.TenMacBeTong = reader["TenMacBeTong"].ToString();
                    macbetong.BeTongCot = float.Parse(reader["BeTongCot"].ToString());
                    macbetong.BeTongSan = float.Parse(reader["BeTongSan"].ToString());
                    macbetong.ModuleDanHoiBeTongCot = float.Parse(reader["ModuleDanHoiBeTongCot"].ToString());
                    macbetong.ModuleDanHoiBeTongSan = float.Parse(reader["ModuleDanHoiBeTongSan"].ToString());
                }
                return macbetong;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacBuLong FindMacBuLong(int id)
        {
            try
            {
                Connect();
                MacBuLong macbulong = new MacBuLong();
                cmd.CommandText = "SELECT * FROM MacBuLong WHERE MacBuLongID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    macbulong.MacBuLongID = int.Parse(reader["MacBuLongID"].ToString());
                    macbulong.TenMacBuLong = reader["TenMacBuLong"].ToString();
                    macbulong.BuLong = float.Parse(reader["BuLong"].ToString());
                    
                }
                return macbulong;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public MacThepSan FindMacThepSan(int id)
        {
            try
            {
                Connect();
                MacThepSan macthepsan = new MacThepSan();
                cmd.CommandText = "SELECT * FROM MacThepSan WHERE MacThepSanID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    macthepsan.MacThepSanID = int.Parse(reader["MacThepSanID"].ToString());
                    macthepsan.TenMacThepSan = reader["TenMacThepSan"].ToString();
                    macthepsan.CotThepSan = float.Parse(reader["CuongDoCotThepSan"].ToString());
                    macthepsan.ModuleDanHoiCotThepSan = float.Parse(reader["ModuleDanHoiCotThepSan"].ToString());

                }
                return macthepsan;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public ThongSoLienKet FindThongSoLienKet(int id)
        {
            try
            {
                Connect();
                ThongSoLienKet thongso = new ThongSoLienKet();
                cmd.CommandText = "SELECT * FROM ThongSoLienKet WHERE ThongSoLienKetID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thongso.ThongSoLienKetID = int.Parse(reader["ThongSoLienKetID"].ToString());
                    thongso.TenThongSo = reader["TenThongSo"].ToString();
                    thongso.ChieuCaoDuongHanBung = float.Parse(reader["ChieuCaoDuongHanBung"].ToString());
                    thongso.ChieuCaoDuongHanCanh = float.Parse(reader["ChieuCaoDuongHanCanh"].ToString());
                    thongso.BuLongDenMepTrenSuon = float.Parse(reader["BuLongDenMepTrenSuon"].ToString());
                    thongso.BuLongDenMepDuoiSuon = float.Parse(reader["BuLongDenMepDuoiSuon"].ToString());
                    thongso.BuLongDenMepNgoaiCanhCot = float.Parse(reader["BuLongDenMepNgoaiCanhCot"].ToString());
                    thongso.BuLongPhuongNgang = float.Parse(reader["BuLongPhuongNgang"].ToString());
                    thongso.BuLongPhuongDung = float.Parse(reader["BuLongPhuongDung"].ToString());
                    thongso.BeRongSuon = float.Parse(reader["BeRongSuon"].ToString());
                    thongso.ChieuDaySuon = float.Parse(reader["ChieuDaySuon"].ToString());
                    thongso.DienTichBuLong = float.Parse(reader["DienTichBuLong"].ToString());
                    thongso.ChieuDayMu = float.Parse(reader["ChieuDayMu"].ToString());
                    thongso.ChieuDayDaiOc = float.Parse(reader["ChieuDayDaiOc"].ToString());
                    thongso.DuongKinhTamDem = float.Parse(reader["DuongKinhTamDem"].ToString());
                    thongso.ChieuDayBeTong = float.Parse(reader["ChieuDayBeTong"].ToString());
                    thongso.ChieuCaoMuiTon = float.Parse(reader["ChieuCaoMuiTon"].ToString());
                    thongso.ChieuDayLopBaoVe = float.Parse(reader["ChieuDayLopBaoVe"].ToString());
                    thongso.HamLuongCotThep = float.Parse(reader["HamLuongCotThep"].ToString());
                    thongso.DoCung = float.Parse(reader["DoCung"].ToString());
                    thongso.KhoangCach = float.Parse(reader["KhoangCach"].ToString());
                }
                return thongso;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public DieuKienTaiTrong FindDieuKienTaiTrong(int id)
        {
            try
            {
                Connect();
                DieuKienTaiTrong dieukien = new DieuKienTaiTrong();
                cmd.CommandText = "SELECT * FROM DieuKienTaiTrong WHERE DieuKienTaiTrongID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    dieukien.DieuKienTaiTrongID = int.Parse(reader["DieuKienTaiTrongID"].ToString());
                    dieukien.TenDieuKienTaiTrong = reader["TenDieuKienTaiTrong"].ToString();
                    dieukien.GiaTriBeta = int.Parse(reader["GiaTriBeta"].ToString());
                    dieukien.GiaTri = int.Parse(reader["GiaTri"].ToString());
                }
                return dieukien;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public DieuKienCauTaoCot FindDieuKienCauTaoCot(int id)
        {
            try
            {
                Connect();
                DieuKienCauTaoCot dieukien = new DieuKienCauTaoCot();
                cmd.CommandText = "SELECT * FROM DieuKienCauTaoCot WHERE DieuKienCauTaoID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    dieukien.DieuKienCauTaoCotID = int.Parse(reader["DieuKienCauTaoID"].ToString());
                    dieukien.TenDieuKien = reader["TenDieuKien"].ToString();
                    dieukien.GiaTri = int.Parse(reader["GiaTri"].ToString());
                }
                return dieukien;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public KetQua FindKetQua(int id) {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE KetQuaID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                    ketqua.LoaiThepCotId = int.Parse(reader["LoaiThepCotID"].ToString());
                    ketqua.LoaiThepDamId = int.Parse(reader["LoaiThepDamID"].ToString());
                    ketqua.DieuKienTaiTrongID = int.Parse(reader["DieuKienTaiTrongID"].ToString());
                    ketqua.HeSoAnToanId = int.Parse(reader["HeSoAnToanID"].ToString());
                    ketqua.MacBeTongId = int.Parse(reader["MacBeTongID"].ToString());
                    ketqua.MacBuLongId = int.Parse(reader["MacBuLongID"].ToString());
                    ketqua.MacKetCauId = int.Parse(reader["MacThepKetCauID"].ToString());
                    ketqua.MacThepSanId = int.Parse(reader["MacThepSanID"].ToString());
                    ketqua.ThongSoLienKetId = int.Parse(reader["ThongSoLienKetID"].ToString());
                    ketqua.DieuKienCauTaoCotID = int.Parse(reader["DieuKienCauTaoCotID"].ToString());
                    ketqua.K1b = float.Parse(reader["K1b"].ToString());
                    ketqua.K1c = float.Parse(reader["K1c"].ToString());
                    ketqua.K2b = float.Parse(reader["K2b"].ToString());
                    ketqua.K2c = float.Parse(reader["K2c"].ToString());
                    ketqua.K3b = float.Parse(reader["K3b"].ToString());
                    ketqua.K3c = float.Parse(reader["K3c"].ToString());
                    ketqua.K4b = float.Parse(reader["K4b"].ToString());
                    ketqua.K4c = float.Parse(reader["K4c"].ToString());
                    ketqua.K5b = float.Parse(reader["K5b"].ToString());
                    ketqua.K5c = float.Parse(reader["K5c"].ToString());
                    ketqua.K7b = float.Parse(reader["K7b"].ToString());
                    ketqua.K8b = float.Parse(reader["K8b"].ToString());
                    ketqua.K9c = float.Parse(reader["K9c"].ToString());
                    ketqua.K10b = float.Parse(reader["K10b"].ToString());
                    ketqua.K10c = float.Parse(reader["K10c"].ToString());
                    ketqua.K13b = float.Parse(reader["K13b"].ToString());
                    ketqua.K13c = float.Parse(reader["K13c"].ToString());
                    ketqua.Mrd = float.Parse(reader["Mrd"].ToString());
                    ketqua.Sj = float.Parse(reader["Sj"].ToString());
                    ketqua.HeSoAlpha = float.Parse(reader["HeSoAlpha"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region DataTable

        public DataTable LoadLoaiThepCot()
        {
            Connect();
            DataTable loaithep = new DataTable();
            loaithep.Columns.Add("LoaiThepCotID", typeof(int));
            loaithep.Columns.Add("TenLoaiThepCot", typeof(string));

            cmd.CommandText = "SELECT * FROM LoaiThepCot";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = loaithep.NewRow();
                dr["LoaiThepCotID"] = int.Parse(reader["LoaiThepCotID"].ToString());
                dr["TenLoaiThepCot"] = reader["TenLoaiThepCot"].ToString();
                loaithep.Rows.Add(dr);
            }
            return loaithep;

        }

        public DataTable LoadLoaiThepDam()
        {
            Connect();
            DataTable loaithep = new DataTable();
            loaithep.Columns.Add("LoaiThepDamID", typeof(int));
            loaithep.Columns.Add("TenLoaiThepDam", typeof(string));

            cmd.CommandText = "SELECT * FROM LoaiThepDam";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = loaithep.NewRow();
                dr["LoaiThepDamID"] = int.Parse(reader["LoaiThepDamID"].ToString());
                dr["TenLoaiThepDam"] = reader["TenLoaiThepDam"].ToString();
                loaithep.Rows.Add(dr);
            }

            return loaithep;

        }

        public DataTable LoadMacThepKetCau() {
            Connect();
            DataTable Macthep = new DataTable();
            Macthep.Columns.Add("MacThepKetCauID", typeof(int));
            Macthep.Columns.Add("TenMacThep", typeof(string));

            cmd.CommandText = "SELECT * FROM MacThepKetCau";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = Macthep.NewRow();
                dr["MacThepKetCauID"] = int.Parse(reader["MacThepKetCauID"].ToString());
                dr["TenMacThep"] = reader["TenMacThep"].ToString();
                Macthep.Rows.Add(dr);
            }
            conn.Close();
            return Macthep;
        }

        public DataTable LoadHeSoAnToan() {
            Connect();
            DataTable heso = new DataTable();
            heso.Columns.Add("HeSoAnToanID", typeof(int));
            heso.Columns.Add("TenHeSo", typeof(string));

            cmd.CommandText = "SELECT * FROM HeSoAnToan";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = heso.NewRow();
                dr["HeSoAnToanID"] = int.Parse(reader["HeSoAnToanID"].ToString());
                dr["TenHeSo"] = reader["TenHeSo"].ToString();
                heso.Rows.Add(dr);
            }
            conn.Close();
            return heso;
        }

        public DataTable LoadMacBeTong() {
            Connect();
            DataTable macbetong = new DataTable();
            macbetong.Columns.Add("MacBeTongID", typeof(int));
            macbetong.Columns.Add("TenMacBeTong", typeof(string));

            cmd.CommandText = "SELECT * FROM MacBeTong";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = macbetong.NewRow();
                dr["MacBeTongID"] = int.Parse(reader["MacBeTongID"].ToString());
                dr["TenMacBeTong"] = reader["TenMacBeTong"].ToString();
                macbetong.Rows.Add(dr);
            }
            return macbetong;
        }

        public DataTable LoadMacBuLong() {
            Connect();
            DataTable macbulong = new DataTable();
            macbulong.Columns.Add("MacBuLongID", typeof(int));
            macbulong.Columns.Add("TenMacBuLong", typeof(string));

            cmd.CommandText = "SELECT * FROM MacBuLong";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = macbulong.NewRow();
                dr["MacBuLongID"] = int.Parse(reader["MacBuLongID"].ToString());
                dr["TenMacBuLong"] = reader["TenMacBuLong"].ToString();
                macbulong.Rows.Add(dr);
            }

            return macbulong;
        }

        public DataTable LoadMacThepSan() {
            Connect();
            DataTable macthepsan = new DataTable();
            macthepsan.Columns.Add("TenMacThepSan", typeof(string));
            macthepsan.Columns.Add("MacThepSanID", typeof(int));

            cmd.CommandText = "SELECT * FROM MacThepSan";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = macthepsan.NewRow();
                dr["TenMacThepSan"] = reader["TenMacThepSan"].ToString();
                dr["MacThepSanID"] = int.Parse(reader["MacThepSanID"].ToString());
                macthepsan.Rows.Add(dr);
            }
            return macthepsan;
        }

        public DataTable LoadThongSoLienKet() {
            Connect();
            DataTable thongso = new DataTable();
            thongso.Columns.Add("ThongSoLienKetID", typeof(int));
            thongso.Columns.Add("TenThongSo", typeof(string));

            cmd.CommandText = "SELECT * FROM ThongSoLienKet";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = thongso.NewRow();
                dr["ThongSoLienKetID"] = int.Parse(reader["ThongSoLienKetID"].ToString());
                dr["TenThongSo"] = reader["TenThongSo"].ToString();
                thongso.Rows.Add(dr);
            }
            return thongso;
        }

        public DataTable LoadDieuKienTaiTrong() {
            Connect();
            DataTable thongso = new DataTable();
            thongso.Columns.Add("TenDieuKienTaiTrong", typeof(string));
            thongso.Columns.Add("DieuKienTaiTrongID", typeof(int));

            cmd.CommandText = "SELECT * FROM DieuKienTaiTrong";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = thongso.NewRow();
                dr["TenDieuKienTaiTrong"] = reader["TenDieuKienTaiTrong"].ToString();
                dr["DieuKienTaiTrongID"] = int.Parse( reader["DieuKienTaiTrongID"].ToString());
                thongso.Rows.Add(dr);
            }
            return thongso;
        }

        public DataTable LoadDieuKienCauTaoCot() {
            Connect();
            DataTable thongso = new DataTable();
            thongso.Columns.Add("TenDieuKien", typeof(string));
            thongso.Columns.Add("DieuKienCauTaoID", typeof(int));

            cmd.CommandText = "SELECT * FROM DieuKienCauTaoCot";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = thongso.NewRow();
                dr["TenDieuKien"] = reader["TenDieuKien"].ToString();
                dr["DieuKienCauTaoID"] = int.Parse( reader["DieuKienCauTaoID"].ToString());
                thongso.Rows.Add(dr);
            }
            return thongso;
        }

        public DataTable LoadketQua() {
            Connect();
            DataTable ketqua = new DataTable();
            ketqua.Columns.Add("KetQuaID", typeof(int));
            ketqua.Columns.Add("TenLoaiThepCot", typeof(string));
            ketqua.Columns.Add("TenLoaiThepDam", typeof(string));
            ketqua.Columns.Add("HeSoAlpha", typeof(float));
            ketqua.Columns.Add("TenHeSoAnToan", typeof(string));
            ketqua.Columns.Add("TenMacBeTong", typeof(string));
            ketqua.Columns.Add("TenMacBuLong", typeof(string));
            ketqua.Columns.Add("TenMacThepKetCau", typeof(string));
            ketqua.Columns.Add("TenMacThepSan", typeof(string));
            ketqua.Columns.Add("TenThongSoLienKet", typeof(string));

            cmd.CommandText = "SELECT * FROM KetQua";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataRow dr = ketqua.NewRow();
                dr["KetQuaID"] = reader["KetQuaID"];
                dr["TenLoaiThepCot"] = FindLoaiThepCot(int.Parse(reader["LoaiThepCotID"].ToString())).TenLoaiThepCot;
                dr["TenLoaiThepDam"] = FindLoaiThepDam(int.Parse(reader["LoaiThepDamID"].ToString())).TenLoaiThepDam;
                dr["HeSoAlpha"] = float.Parse( reader["HeSoAlpha"].ToString() );
                dr["TenHeSoAnToan"] = FindHeSo( int.Parse( reader["HeSoAnToanID"].ToString() ) ).TenHeSo;
                dr["TenMacBeTong"] = FindMacBeTong(int.Parse(reader["MacBeTongID"].ToString())).TenMacBeTong;
                dr["TenMacBuLong"] = FindMacBuLong(int.Parse(reader["MacBuLongID"].ToString())).TenMacBuLong;
                dr["TenMacThepKetCau"] = FindMacThep(int.Parse(reader["MacThepKetCauID"].ToString())).TenMacThep;
                dr["TenMacThepSan"] = FindMacThepSan(int.Parse(reader["MacThepSanID"].ToString())).TenMacThepSan;
                dr["TenThongSoLienKet"] = FindThongSoLienKet(int.Parse(reader["ThongSoLienKetID"].ToString())).TenThongSo;
                ketqua.Rows.Add(dr);
            }
            return ketqua;
        }

        #endregion

        public void Delete(int id)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE KetQuaID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public void InsertKetQua(KetQua kq) {
            try
            {
                string string1 = "INSERT INTO KetQua(LoaiThepCotID,"+
                                                    "LoaiThepDamID,"+
                                                    "DieuKienTaiTrongID,"+
                                                    "HeSoAnToanID,"+
                                                    "MacBeTongID,"+
                                                    "MacBuLongID,"+
                                                    "MacThepKetCauID,"+
                                                    "MacThepSanID,"+
                                                    "ThongSoLienKetID,"+
                                                    "DieuKienCauTaoCotID,"+
                                                    "K1b,"+
                                                    "K1c,"+
                                                    "K2b,"+
                                                    "K2c,"+
                                                    "K3b,"+
                                                    "K3c,"+
                                                    "K4b,"+
                                                    "K4c,"+
                                                    "K5b,"+
                                                    "K5c,"+
                                                    "K7b,"+
                                                    "K8b,"+
                                                    "K9c,"+
                                                    "K10b,"+
                                                    "K10c,"+
                                                    "K13b,"+
                                                    "K13c,"+
                                                    "Mrd," + 
                                                    "Sj," +
                                                    "HeSoAlpha" +
                                                    ") "+

                                "VALUES(" + 
                                kq.LoaiThepCotId + "," +
                                kq.LoaiThepDamId + "," + 
                                kq.DieuKienTaiTrongID + "," + 
                                kq.HeSoAnToanId + "," + 
                                kq.MacBeTongId + "," +
                                kq.MacBuLongId + "," +
                                kq.MacKetCauId +"," +
                                kq.MacThepSanId +"," +
                                kq.ThongSoLienKetId +"," +
                                kq.DieuKienCauTaoCotID +"," +
                                kq.K1b +"," +
                                kq.K1c +"," +
                                kq.K2b +"," +
                                kq.K2c +"," +
                                kq.K3b +"," +
                                kq.K3c +"," +
                                kq.K4b +"," +
                                kq.K4c +"," +
                                kq.K5b +"," +
                                kq.K5c +"," +
                                kq.K7b +"," +
                                kq.K8b +"," +
                                kq.K9c +"," +
                                kq.K10b +"," +
                                kq.K10c +"," +
                                kq.K13b +"," +
                                kq.K13c +"," +
                                kq.Mrd + "," + 
                                kq.Sj + "," +
                                kq.HeSoAlpha +
                                 ")";
                cmd.CommandText = string1;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void ActiveButton(bool flag){
            btnLayKetQua.Enabled = flag;
            btnNhapDuLieu.Enabled = !flag;
        }

        public void ActiveCombobox(bool flag) {
            luCot.Enabled = luDam.Enabled = luDieuKienCauTaoCot.Enabled = luDieuKienTaiTrong.Enabled = luHeSoAnToan.Enabled = luMacBeTong.Enabled = luMacBuLong.Enabled = luMacThepKetCau.Enabled = luMacThepSan.Enabled = luThongSolienKet.Enabled = flag;
            grcDoBen.Enabled = grcKetQua.Enabled = !flag;

        }

        public void LoadData() {
            luCot.Properties.DataSource = LoadLoaiThepCot();
            luDam.Properties.DataSource = LoadLoaiThepDam();

            luMacBeTong.Properties.DataSource = LoadMacBeTong();
            luMacBuLong.Properties.DataSource = LoadMacBuLong();
            luMacThepSan.Properties.DataSource = LoadMacThepSan();
            luMacThepKetCau.Properties.DataSource = LoadMacThepKetCau();

            luHeSoAnToan.Properties.DataSource = LoadHeSoAnToan();
            luThongSolienKet.Properties.DataSource = LoadThongSoLienKet();
            luDieuKienTaiTrong.Properties.DataSource = LoadDieuKienTaiTrong();
            luDieuKienCauTaoCot.Properties.DataSource = LoadDieuKienCauTaoCot();
        }

        public void ComboBoxNull() {
            luCot.EditValue = luDam.EditValue = luDieuKienCauTaoCot.EditValue = luDieuKienTaiTrong.EditValue = luHeSoAnToan.EditValue = luMacBeTong.EditValue = luMacBuLong.EditValue = luMacThepKetCau.EditValue = luMacThepSan.EditValue = luThongSolienKet.EditValue = null;
        }

        public float MIN(float a, float b) {
            return a < b ? a : b;
        }

        public DataTable CreateTable() {
            DataTable tableKetQua = new DataTable();
            tableKetQua.Columns.Add("Ten", typeof(string));
            tableKetQua.Columns.Add("Ben", typeof(string));
            tableKetQua.Columns.Add("Cung", typeof(string));

            tableKetQua.Rows.Add("k1 (bụng cột chịu cắt)", "0", "0");
            tableKetQua.Rows.Add("k2 (bụng cột chịu nén)", "0", "0");
            tableKetQua.Rows.Add("k3 (bụng cột chịu kéo)", "0", "0");
            tableKetQua.Rows.Add("k4 (cánh cột chịu uốn)", "0", "0");
            tableKetQua.Rows.Add("k5(bản sườn đầu dầm chịu uốn)", "0", "0");
            tableKetQua.Rows.Add("k6 (bụng cột chịu uốn)", "0", "0");
            tableKetQua.Rows.Add("k7 (cánh dầm chịu nén)", "0", "0");
            tableKetQua.Rows.Add("k8 (bụng dầm chịu kéo)", "0", "0");
            tableKetQua.Rows.Add("k9 (thép góc liên kết chịu uốn)", "0", "0");
            tableKetQua.Rows.Add("k10 (bu lông chịu kéo)", "0", "0");
            tableKetQua.Rows.Add("k11 (bu lông chịu cắt)", "0", "0");
            tableKetQua.Rows.Add("k12 (bu lông chịu ép mặt)", "0", "0");
            tableKetQua.Rows.Add("k13 (cốt thép trong bản sàn chịu kéo)", "0", "0");
            tableKetQua.Rows.Add("", "", "");
            tableKetQua.Rows.Add("Đánh giá đặc chưng cơ học liên kết", "", "");
            tableKetQua.Rows.Add("MRd =", "0", "KN.m");
            tableKetQua.Rows.Add("Sj,ini = ", "0", "KN.m/rad");

            return tableKetQua;
        }

        public DataTable CreateTable(KetQua ketqua) {
            DataTable tableKetQua = new DataTable();
            tableKetQua.Columns.Add("Ten", typeof(string));
            tableKetQua.Columns.Add("Ben", typeof(string));
            tableKetQua.Columns.Add("Cung", typeof(string));

            tableKetQua.Rows.Add("k1 (bụng cột chịu cắt)", ketqua.K1b, ketqua.K1c);
            tableKetQua.Rows.Add("k2 (bụng cột chịu nén)", ketqua.K2b, ketqua.K2c);
            tableKetQua.Rows.Add("k3 (bụng cột chịu kéo)", ketqua.K3b, ketqua.K3c);
            tableKetQua.Rows.Add("k4 (cánh cột chịu uốn)", ketqua.K4b, ketqua.K4c);
            tableKetQua.Rows.Add("k5(bản sườn đầu dầm chịu uốn)", ketqua.K5b, ketqua.K5c);
            tableKetQua.Rows.Add("k6 (bụng cột chịu uốn)", "∞", "0");
            tableKetQua.Rows.Add("k7 (cánh dầm chịu nén)", ketqua.K7b, "0");
            tableKetQua.Rows.Add("k8 (bụng dầm chịu kéo)", ketqua.K8b, "0");
            tableKetQua.Rows.Add("k9 (thép góc liên kết chịu uốn)", "∞", ketqua.K9c);
            tableKetQua.Rows.Add("k10 (bu lông chịu kéo)", ketqua.K10b, ketqua.K10c);
            tableKetQua.Rows.Add("k11 (bu lông chịu cắt)", "∞", "0");
            tableKetQua.Rows.Add("k12 (bu lông chịu ép mặt)", "∞", "0");
            tableKetQua.Rows.Add("k13 (cốt thép trong bản sàn chịu kéo)", ketqua.K13b, ketqua.K13c);
            tableKetQua.Rows.Add("", "", "");
            tableKetQua.Rows.Add("Đánh giá đặc chưng cơ học liên kết", "", "");
            tableKetQua.Rows.Add("MRd =", ketqua.Mrd, "KN.m");
            tableKetQua.Rows.Add("Sj,ini = ", ketqua.Sj, "KN.m/rad");
            return tableKetQua;
        }

        #endregion

        #region FormEvent

        private void FrmKetQua_Load(object sender, EventArgs e)
        {
            Connect();
            LoadData();

            btnHuy.Enabled = false;
            ActiveButton(false);
            ActiveCombobox(false);
            btnXoa.Enabled = false;
            btnXuat.Enabled = false;

            lbModuleDanHoiBeTong.Text = lbModuleDanHoiThepKetCau.Text = "0 (N/mm2)";
            lbBeta.Text = "0";

            

            grcKetQua.DataSource = CreateTable();
            grcDoBen.DataSource = LoadketQua();
        }

        private void btnLayKetQua_Click(object sender, EventArgs e)
        {
            Connect();

            if(luCot.EditValue == null ||
               luDam.EditValue == null ||
                luDieuKienCauTaoCot.EditValue == null ||
                luDieuKienTaiTrong.EditValue == null ||
                luHeSoAnToan.EditValue == null ||
                luMacBeTong.EditValue == null ||
                luMacBuLong.EditValue == null ||
                luMacThepKetCau.EditValue == null ||
                luMacThepSan.EditValue == null ||
                luThongSolienKet.EditValue == null 
                ){

              if (luCot.EditValue == null) { luCot.ErrorText = "Bạn phải chọn loại thép cho cột"; return; }
              if (luDam.EditValue == null) { luDam.ErrorText = "Bạn phải chọn loại thép cho dầm"; return; }
              if (luDieuKienCauTaoCot.EditValue == null) { luDieuKienCauTaoCot.ErrorText = "Bạn phải chọn điều kiện cấu tạo cột"; return; }
              if (luDieuKienTaiTrong.EditValue == null) { luDieuKienTaiTrong.ErrorText = "Bạn phải chọn điều kiện tải trọng"; return; }
              if (luHeSoAnToan.EditValue == null) { luHeSoAnToan.ErrorText = "Bạn phải chọn hệ số an toàn"; return; }
              if (luMacBeTong.EditValue == null) { luMacBeTong.ErrorText = "bạn phải chọn mác bê tông"; return; }
              if (luMacBuLong.EditValue == null) { luMacBuLong.ErrorText = "Bạn phải chọn mác bu lông"; return; }
              if (luMacThepKetCau.EditValue == null) { luMacThepKetCau.ErrorText = "Bạn phải chọn thông số mác thép kết cấu"; return; }
              if (luMacThepSan.EditValue == null) { luMacThepSan.ErrorText = "Bạn phải chọn thông số mác thép sàn"; return; }
             
              if (luThongSolienKet.EditValue == null) { luThongSolienKet.ErrorText = "Bạn phải chọn thông số"; return; }
                
            }

            KetQua ketqua = new KetQua();
            LoaiThepCot cot = FindLoaiThepCot((int)luCot.EditValue);
            ketqua.LoaiThepCotId = cot.LoaiThepCotID;

            LoaiThepDam dam = FindLoaiThepDam((int)luDam.EditValue);
            ketqua.LoaiThepDamId = dam.LoaiThepDamID;

            MacThepKetCau macthepketcau = FindMacThep((int)luMacThepKetCau.EditValue);
            ketqua.MacKetCauId = macthepketcau.MacThepKetCauID;

            MacBeTong macbetong = FindMacBeTong((int)luMacBeTong.EditValue);
            ketqua.MacBeTongId = macbetong.MacBeTongID;

            MacThepSan macthepsan = FindMacThepSan((int)luMacThepSan.EditValue);
            ketqua.MacThepSanId = macthepsan.MacThepSanID;

            DieuKienCauTaoCot dieukiencot = FindDieuKienCauTaoCot((int)luDieuKienCauTaoCot.EditValue);
            ketqua.DieuKienCauTaoCotID = dieukiencot.DieuKienCauTaoCotID;

            DieuKienTaiTrong dieukientaitrong = FindDieuKienTaiTrong((int)luDieuKienTaiTrong.EditValue);
            ketqua.DieuKienTaiTrongID = dieukientaitrong.DieuKienTaiTrongID;

            HeSoAnToan heso = FindHeSo((int)luHeSoAnToan.EditValue);
            ketqua.HeSoAnToanId = heso.HeSoAnToanID;

            ThongSoLienKet thongso = FindThongSoLienKet((int)luThongSolienKet.EditValue);
            ketqua.ThongSoLienKetId = thongso.ThongSoLienKetID;

            MacBuLong macbulong = FindMacBuLong((int)luMacBuLong.EditValue);
            ketqua.MacBuLongId = macbulong.MacBuLongID;
                        
            
            

            




            //so lieu tinh toan
            
            float n29 = cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh - 2 * cot.ChieuCaoDuongHan;
            float n30 = cot.ThepToHop == 1 ? (cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh) * cot.ChieuDayBanBung : cot.DienTichTietDienCotThep - 2 * cot.BeRongBanCanh * cot.ChieuDayBanCanh + (cot.ChieuDayBanBung + 2 * cot.ChieuCaoDuongHan) * cot.ChieuDayBanCanh;
            float n31 = (float) (((thongso.BuLongPhuongNgang - cot.ChieuDayBanBung) / 2) - 0.8 * cot.ChieuCaoDuongHan);
            float n32 = (cot.BeRongBanCanh - thongso.BuLongPhuongNgang) / 2; 
            float n33 = (float) (0.25 * cot.ChieuDayBanCanh * cot.ChieuDayBanCanh * macthepketcau.CanhCot / heso.KetCauThep);
            float n34 = cot.ChieuCaoDuongHan; 
            float n36 = (float) ( 0.5 * dam.ChieuCaoTietDien + thongso.ChieuDayBeTong + thongso.ChieuCaoMuiTon - thongso.ChieuDayLopBaoVe);
            float n37 = (float) ( dam.ChieuCaoTietDien - 0.5 * dam.ChieuDayBanCanh + thongso.ChieuDayBeTong + thongso.ChieuCaoMuiTon - thongso.ChieuDayLopBaoVe);
            float n38 = (float) (thongso.BuLongPhuongDung + thongso.BuLongDenMepDuoiSuon - 0.5 * dam.ChieuDayBanCanh); 
            float n39 = n38 / n37; 
            float n40 = cot.BeRongBanCanh * (thongso.BuLongPhuongDung / 2) * (thongso.BuLongPhuongDung / 2) - (thongso.BuLongPhuongDung / 2) * (thongso.BuLongPhuongDung / 2) * cot.ChieuDayBanBung;
            float n41 = n40 * macthepketcau.CanhDam / (1000000 * heso.KetCauThep); 
            float n43 = (float)((thongso.BuLongPhuongNgang - dam.ChieuDayBanBung) / 2 - 0.8 * thongso.ChieuCaoDuongHanBung * System.Math.Sqrt(2));
            float n44 = (float) (thongso.BuLongDenMepTrenSuon - dam.ChieuDayBanCanh - 0.8 * thongso.ChieuCaoDuongHanCanh * System.Math.Sqrt(2)); 
            float n45 = n43 / (n43 + thongso.BuLongDenMepNgoaiCanhCot); 
            float n46 = n44 / (n44 + thongso.BuLongDenMepNgoaiCanhCot);
            
            float n47;
            FrmGetAlpha frm = new FrmGetAlpha(n45,n46);
            DialogResult rs = frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK) n47 = Const.Alpha;
            else {
                MessageBox.Show("Không thể tính toán tiếp do thiếu số liệu!");
                ActiveCombobox(false);
                ActiveButton(false);
                luCot.EditValue = luDam.EditValue = luDieuKienCauTaoCot.EditValue = luDieuKienTaiTrong.EditValue = luHeSoAnToan.EditValue = luMacBeTong.EditValue = luMacBuLong.EditValue = luMacThepKetCau.EditValue = luMacThepSan.EditValue = luThongSolienKet.EditValue = null;
                ketquaID = "";
                lbBeta.Text = "0";
                grcDoBen.DataSource = LoadketQua();
                btnXoa.Enabled = false;

                return;
            }
            ketqua.HeSoAlpha = n47;
            float n48 = (float)(0.25 * thongso.ChieuDaySuon * thongso.ChieuDaySuon * macthepketcau.TamDem / heso.KetCauThep);
            float n50 = (float)(0.9 * macbulong.BuLong * thongso.DienTichBuLong * 0.001 / heso.BuLong);
            float n51 = (float)(0.6 * macbulong.BuLong * thongso.DienTichBuLong * 0.001 / heso.BuLong);
            float n52 = (float)(cot.ChieuDayBanCanh + thongso.ChieuDaySuon + 0.5 * (thongso.ChieuDayMu + thongso.ChieuDayDaiOc));
            float n53 = thongso.DuongKinhTamDem / 4; 
            float n55 = thongso.ChieuDayBeTong;
            float n56 = 3 * dam.ChieuCaoTietDien;
            float n57 = 4 * dam.ChieuCaoTietDien; 
            float n58 = thongso.HamLuongCotThep * n55 * (n56 - cot.BeRongBanCanh) / 100;
            float n60 = 1 + n57 / thongso.KhoangCach;
            
            //k2
            float p29 = dam.ChieuDayBanCanh / 2 + thongso.ChieuCaoDuongHanCanh * ((float)System.Math.Sqrt(2)) + thongso.ChieuDaySuon;
            float p30 = p29 + 5 * cot.ChieuDayBanCanh;
            float p31 = p29 + 5 * (cot.ChieuCaoDuongHan + cot.ChieuDayBanCanh);
            float p32 = p29 + 2 * (cot.ChieuCaoDuongHan + cot.ChieuDayBanCanh);
            float p33 = (float)(0.5 * macthepketcau.BungCot); 
            float p34 = MIN( (float)1.0 , (float)(1.25 - 0.5 * p33 / macthepketcau.BungCot));
            float p36 =(float) (0.932 * System.Math.Sqrt((p31 * n29 * macthepketcau.BungCot / (cot.ChieuDayBanBung * cot.ChieuDayBanBung * macthepketcau.ModuleDanHoiThepKetCau * 1000))));
            float p37 = (float)(1 / System.Math.Sqrt((1 + 1.3 * (p31 * cot.ChieuDayBanBung / n30) * (p31 * cot.ChieuDayBanBung / n30))));
            
            float p38;
            if (dieukientaitrong.GiaTri == 1 || dieukientaitrong.GiaTri == 4) p38 = p37; else p38 = 1; 

            float p39 = p34 * p38 * p31 * cot.ChieuDayBanBung * macthepketcau.BungCot / (heso.KetCauThep * 1000);
            float p40 = 1;
            float p41 = p34 * p40 * p31 * cot.ChieuDayBanBung * macthepketcau.BungCot / (1000 * heso.KetCauThep);
            float p43 = MIN( 2 , (float) (1.3 + 3.3 * p33 / (macbetong.BeTongCot / heso.BeTong)) ); 
            float p44 = (float) (0.85 * p43 * p30 * (cot.BeRongBanCanh - cot.ChieuDayBanBung) * macbetong.BeTongCot / (1000 * heso.BeTong));
            ketqua.K2b = dieukiencot.GiaTri == 1? p39 : p41 + p44;
            
            float p48 = (float)(0.7 * p31 * cot.ChieuDayBanBung / n29); 
            float p49 = (float)(0.5 * p32 * cot.BeRongBanCanh * macbetong.ModuleDanHoiBeTongCot / (macthepketcau.ModuleDanHoiThepKetCau * cot.ChieuCaoTietDien)); 
            ketqua.K2c = dieukiencot.GiaTri == 1? p48 : p48 + p49; 

            //k3
            float p53 = MIN((2 * (float)System.Math.PI * n31),( 4 * n31 + (float)1.25 * n32));
            float p54 = (float)( 1 / System.Math.Sqrt((1 + 1.3 * ((p31) * cot.ChieuDayBanBung / n30) * (p31 * cot.ChieuDayBanBung / n30)))); // fixed
            float p55;
            if (dieukientaitrong.GiaTri == 1 || dieukientaitrong.GiaTri == 4) p55 = p54; else p55 = 1; 
            ketqua.K3b = (p55 * p53 * cot.ChieuDayBanBung * macthepketcau.BungCot) / (1000 * heso.KetCauThep);
            ketqua.K3c = (float)(0.7 * p53 * cot.ChieuDayBanBung / n29);
            //k4
            float p62 = p53;
            float p63 = MIN(n32, MIN((float)(1.25 * n31), thongso.BuLongDenMepNgoaiCanhCot));
            float p64 = (float) (0.5 * macthepketcau.CanhCot);
            float p65 = MIN(1, (2 * macthepketcau.CanhCot - 180 - p64) / (2 * macthepketcau.CanhCot - 360));
            float p66 = (8 * p63 - 2 * n53) * p62 * n33 * p65 / (2 * n31 * p63 - n53 * (n31 + p63));
            float p67 = (p65 * 2 * p62 * n33 + 2 * n50 * p63 * 1000) / (n31 + p63);
            ketqua.K4b = (MIN(p66, p67)) / 1000;
            ketqua.K4c = (float)(0.85 * p62 * cot.ChieuDayBanCanh * cot.ChieuDayBanCanh * cot.ChieuDayBanCanh / (n31 * n31 * n31));
            //k5
            float p73 = MIN((float)(2 * 3.141592654 * n43), n47 * n43);
            float p74 = MIN(thongso.BuLongDenMepNgoaiCanhCot, MIN((float)(1.25 * n43), n32));
            float p75 = (8 * p74 - 2 * n53) * p73 * n48 / (2 * n43 * p74 - n53 * (n43 + p74));
            float p76 = (2 * p73 * n48 + 2 * n50 * p74 * 1000) / (n43 + p74);
            ketqua.K5b = (MIN(p75, p76)) / 1000;
            ketqua.K5c = (float)(0.85 * p73 * thongso.ChieuDaySuon * thongso.ChieuDaySuon * thongso.ChieuDaySuon / (n43 * n43 * n43));
            //k7
            ketqua.K7b = 1000 * n41 / (dam.ChieuCaoTietDien - dam.ChieuDayBanCanh);
            //k8
            ketqua.K8b = p73 * dam.ChieuDayBanBung * macthepketcau.BungDam / (1000 * heso.KetCauThep);
            //k9 = k5
            ketqua.K9c = ketqua.K5c;
            //k10
            ketqua.K10b = 2 * n50;
            ketqua.K10c = (float)(1.6 * thongso.DienTichBuLong / n52);
            //k13
            float r36 = (float)(0.004 * n55 * (n56 - cot.BeRongBanCanh)); 
            float r37 = (float)(1.1 * (0.85 * macbetong.BeTongSan / heso.BeTong) * cot.BeRongBanCanh * n55 / (dieukientaitrong.GiaTriBeta * (macthepsan.CotThepSan / heso.CotThep)));
            float r41;
            float r42;
            float r43;
            float r44;
            float r45;
            float r46;
            float r47;
            if (n58 > r36 && n58 < r37)
            {

                ketqua.K13b = n58 * macthepsan.CotThepSan / (1000 * heso.CotThep);
                r41 = (float)(dieukientaitrong.GiaTriBeta * (4.3 * dieukientaitrong.GiaTriBeta * dieukientaitrong.GiaTriBeta - 8.9 * dieukientaitrong.GiaTriBeta + 7.2));
                r42 = n58 / (cot.ChieuCaoTietDien * ((1 + dieukientaitrong.GiaTriBeta) / 2 + r41));
                r43 = ((cot.ChieuDayBanBung * (cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh) * (cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh) * (cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh) / 12) + (cot.BeRongBanCanh * cot.ChieuDayBanCanh * cot.ChieuDayBanCanh * cot.ChieuDayBanCanh / 12) + ((cot.ChieuCaoTietDien - cot.ChieuDayBanCanh) * (cot.ChieuCaoTietDien - cot.ChieuDayBanCanh) * cot.BeRongBanCanh * cot.ChieuDayBanCanh / 4)) / 10000;
                r44 = macthepketcau.ModuleDanHoiThepKetCau * r43 * 10000 / (macthepsan.ModuleDanHoiCotThepSan * n58 * n36 * n36);
                r45 = (float)(System.Math.Sqrt((1 + r44) * n60 * n36 * n36 * n57 * thongso.DoCung / (10000 * macthepketcau.ModuleDanHoiThepKetCau * r43)));
                r46 = 1000 * n60 * thongso.DoCung / (r45 - (r45 - 1) * n37 / ((r45 + 1) * n36));
                r47 = 1 / (1 + macthepsan.ModuleDanHoiCotThepSan * 1000 * r42 / r46);
                
                ketqua.K13c = r42 * r47;

            }
            else {
                MessageBox.Show("Thông số sai!\nKiểm tra lại thông số đầu vào");
                ActiveCombobox(false);
                ActiveButton(false);
                luCot.EditValue = luDam.EditValue = luDieuKienCauTaoCot.EditValue = luDieuKienTaiTrong.EditValue = luHeSoAnToan.EditValue = luMacBeTong.EditValue = luMacBuLong.EditValue = luMacThepKetCau.EditValue = luMacThepSan.EditValue = luThongSolienKet.EditValue = null;
                ketquaID = "";
                lbBeta.Text = "0";
                grcDoBen.DataSource = LoadketQua();
                btnHuy.Enabled = false;
                btnXoa.Enabled = false;
                return;
            }

            //k1 
            float r52 = (float)0.9 * n30 * macthepketcau.BungCot / (1000 * heso.KetCauThep * (float)System.Math.Sqrt(3)); 
            float r54 = (float)1.1; 
            
            
            float r62 = MIN(ketqua.K2b, ketqua.K7b);
            float r63 = MIN(ketqua.K3b, MIN( MIN( ketqua.K4b, ketqua.K5b) , MIN( ketqua.K8b, ketqua.K10b) ) );
            float r65 = MIN(r62, ketqua.K13b + r63); 
            float r66 = r65 / ketqua.K13b - 1;
            float r67 = r62 > ketqua.K13b ? n37 * (1 + r66 * n39 * n39) / (1 + r66 * n39) : n37;

            float r56 = (float)System.Math.Atan((cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh) / r67) * 180 / (float)System.Math.PI;
            float r57 = (float)System.Math.Abs(0.8 * (cot.ChieuCaoTietDien - 2 * cot.ChieuDayBanCanh) * (cot.BeRongBanCanh - cot.ChieuDayBanBung) * System.Math.Cos(r56));
            float r58 = (float)(r54 * r57 * System.Math.Sin(r56 * System.Math.PI / 180) * 0.85 * macbetong.BeTongCot / (1000 * heso.BeTong));  
            ketqua.K1b = dieukiencot.GiaTri == 1 ? r52 / dieukientaitrong.GiaTriBeta : (r52 + r58) / dieukientaitrong.GiaTriBeta;  

            float r84 = 1 / (1 / ketqua.K3c + 1 / ketqua.K4c + 1 / ketqua.K5c + 1 / ketqua.K10c); 
            float r85 = (ketqua.K13c * n37 * n37 + n38 * n38 * r84) / (ketqua.K13c * n37 + n38 * r84);
            float r86 = (ketqua.K13c * n37 + n38 * r84) / r85;
            float r88 = (float)(0.38 * n30 / (dieukientaitrong.GiaTriBeta * r85));
            float r89 = (float)(0.06 * cot.BeRongBanCanh * cot.ChieuCaoTietDien * macbetong.ModuleDanHoiBeTongCot / (macthepketcau.ModuleDanHoiThepKetCau * dieukientaitrong.GiaTriBeta * r85)); 
            ketqua.K1c = dieukiencot.GiaTri == 1? r88 : r88 + r89;

            float n65 = MIN( MIN(ketqua.K1b, r62), r63 + ketqua.K13b );
            float n66 = n65 * r67 / 1000;

            ketqua.Mrd = 2 * n66 / 3;
            ketqua.Sj = macthepketcau.ModuleDanHoiThepKetCau * r85 * r85 / (1000 * (1 / ketqua.K1c + 1 / ketqua.K2c + 1 / r86));

            InsertKetQua(ketqua);
            ActiveCombobox(false);
            ActiveButton(false);
            luCot.EditValue = luDam.EditValue = luDieuKienCauTaoCot.EditValue = luDieuKienTaiTrong.EditValue = luHeSoAnToan.EditValue = luMacBeTong.EditValue = luMacBuLong.EditValue = luMacThepKetCau.EditValue = luMacThepSan.EditValue = luThongSolienKet.EditValue = null;
            ketquaID = "";
            lbBeta.Text = "0";
            grcDoBen.DataSource = LoadketQua();
            btnXoa.Enabled = false;
        }

        private void btnNhapDuLieu_Click(object sender, EventArgs e)
        {
            Connect();
            LoadData();
            ActiveCombobox(true);
            ActiveButton(true);
            btnHuy.Enabled = true;
            ComboBoxNull();
            grcKetQua.DataSource = CreateTable();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            lbBeta.Text = "0";
            lbModuleDanHoiBeTong.Text = lbModuleDanHoiThepKetCau.Text = "0 (N/mm2)";
            ketquaID = "";

            ActiveButton(false);
            btnXoa.Enabled = false;
            btnHuy.Enabled = false;
            btnXuat.Enabled = false;

            ActiveCombobox(false);
            ComboBoxNull();
            
            grcDoBen.DataSource = LoadketQua();
            grcKetQua.DataSource = CreateTable();
            
        }

        private void grvDoBen_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvDoBen.SelectedRowsCount > 0) {
                KetQua ketqua = FindKetQua((int)grvDoBen.GetRowCellValue(grvDoBen.GetSelectedRows()[0], "KetQuaID"));
                if (ketqua != null) {
                    btnXoa.Enabled = true;
                    ketquaID = ketqua.KetQuaID.ToString();
                    
                    luCot.EditValue = ketqua.LoaiThepCotId;
                    luDam.EditValue = ketqua.LoaiThepDamId;
                    luDieuKienCauTaoCot.EditValue = ketqua.DieuKienCauTaoCotID;
                    luDieuKienTaiTrong.EditValue = ketqua.DieuKienTaiTrongID;
                    luHeSoAnToan.EditValue = ketqua.HeSoAnToanId;
                    luMacBeTong.EditValue = ketqua.MacBeTongId;
                    lbModuleDanHoiBeTong.Text = FindMacBeTong(ketqua.MacBeTongId).ModuleDanHoiBeTongCot.ToString() + " (N/mm2)";
                    luMacBuLong.EditValue = ketqua.MacBuLongId;
                    luMacThepKetCau.EditValue = ketqua.MacKetCauId;
                    lbModuleDanHoiThepKetCau.Text = FindMacThep(ketqua.MacKetCauId).ModuleDanHoiThepKetCau.ToString() + " (N/mm2)";
                    luMacThepSan.EditValue = ketqua.MacThepSanId;
                    luThongSolienKet.EditValue = ketqua.ThongSoLienKetId;
                    lbBeta.Text = FindDieuKienTaiTrong(int.Parse(ketqua.DieuKienTaiTrongID.ToString())).GiaTriBeta.ToString();

                    grcKetQua.DataSource = CreateTable(ketqua);

                    btnHuy.Enabled = true;
                    btnXuat.Enabled = true;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (ketquaID != "") {

                Delete(int.Parse(ketquaID));

                ActiveCombobox(false);
                ActiveButton(false);
                btnXoa.Enabled = false;
                btnHuy.Enabled = false;
                btnXuat.Enabled = false;
                luCot.EditValue = luDam.EditValue = luDieuKienCauTaoCot.EditValue = luDieuKienTaiTrong.EditValue = luHeSoAnToan.EditValue = luMacBeTong.EditValue = luMacBuLong.EditValue = luMacThepKetCau.EditValue = luMacThepSan.EditValue = luThongSolienKet.EditValue = null;
                ketquaID = "";
                lbBeta.Text = "0";
                grcDoBen.DataSource = LoadketQua();
                grcKetQua.DataSource = CreateTable();
            }
        }

        private void luDieuKienTaiTrong_EditValueChanged(object sender, EventArgs e)
        {
            if (luDieuKienTaiTrong.EditValue != null)
            {
                lbBeta.Text = FindDieuKienTaiTrong((int)luDieuKienTaiTrong.EditValue).GiaTriBeta.ToString();
            }
        }

        private void btnXuat_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "Excel";
            saveFile.Filter = "Excel(*.xls)|*.xls";

            DialogResult dialogResult = saveFile.ShowDialog(this);

            //
            //. . .
            //


            if (dialogResult == DialogResult.OK)
            {
                if (saveFile.FileName == "")
                {
                    XtraMessageBox.Show("Tên file không được để trống!", "Thông báo");
                }
                else
                {
                    grvKetQua.ExportToXls(saveFile.FileName + ".xls");
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xuất File Excel Thành Công !!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }

        #endregion

        private void luMacThepKetCau_EditValueChanged(object sender, EventArgs e)
        {
            if (luMacThepKetCau.EditValue != null)
            {
                lbModuleDanHoiThepKetCau.Text = FindMacThep((int)luMacThepKetCau.EditValue).ModuleDanHoiThepKetCau.ToString() + " (N/mm2)";
            }
        }

        private void luMacBeTong_EditValueChanged(object sender, EventArgs e)
        {
            if (luMacBeTong.EditValue != null)
            {
                lbModuleDanHoiBeTong.Text = FindMacBeTong((int)luMacBeTong.EditValue).ModuleDanHoiBeTongCot.ToString() + " (N/mm2)";
            }
        }
 

    }
}
